﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Admin_QuanLyDonDatHang_ThemMoiDonHang : System.Web.UI.Page
{
    private DataAccess da = new DataAccess();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminID"] == null)
        {
            Response.Write("<script language='javascript'>alert('Bạn phải đăng nhập / You must login');location.href='/../Admin/Login.aspx';</script>");
        }
        if (!IsPostBack)
        {
            this.ddlMaHH.Items.Clear();
            FillDropDownList1();
            this.DDL_KH.Items.Clear();
            FillDropDownList2();
            this.txtMaDH.Focus();
        } 
    }

    private void FillDropDownList1()
    {
        SqlCommand comm1 = new SqlCommand("GetAllHang", da.SqlConn());
        comm1.CommandType = CommandType.StoredProcedure;
        SqlDataReader drd1;
        comm1.Connection.Open();
        drd1 = comm1.ExecuteReader();
        while (drd1.Read())
        {
            ListItem ListNhom = new ListItem();
            ListNhom.Text = "" + drd1["TenHang"].ToString();
            ListNhom.Value = drd1["MaHang"].ToString();
            ddlMaHH.Items.Add(ListNhom);
        }
        drd1.Close();
        comm1.Connection.Close();
        comm1.Connection.Dispose();
    }
    private void FillDropDownList2()
    {
        SqlCommand comm1 = new SqlCommand("GetAllKhachHang", da.SqlConn());
        comm1.CommandType = CommandType.StoredProcedure;
        SqlDataReader drd1;
        comm1.Connection.Open();
        drd1 = comm1.ExecuteReader();
        while (drd1.Read())
        {
            ListItem ListNhom = new ListItem();
            ListNhom.Text = ""+ drd1["HoTenKH"].ToString();
            ListNhom.Value = drd1["MaKH"].ToString();
            DDL_KH.Items.Add(ListNhom);
        }
        drd1.Close();
        comm1.Connection.Close();
        comm1.Connection.Dispose();
    }
    protected bool ExistsMaDonHangHang(string strMa)
    {
        bool bOutPut = false;
        SqlDataReader drd = null;
        SqlCommand comm = new SqlCommand("isExistsMaDonHang", da.SqlConn());
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@madonhang", strMa);
        try
        {
            comm.Connection.Open();
            drd = comm.ExecuteReader();
            if (drd.Read())
                bOutPut = true;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message.ToString());
        }
        finally
        {
            comm.Connection.Close();
            comm.Connection.Dispose();
        }
        return bOutPut;
    }

    private void insertDonHang()
    {
        string strMaHang = "";
        string strMaKH = "";
        if (ddlMaHH.SelectedValue.ToString() != "")
        {
            strMaHang = ddlMaHH.SelectedValue.ToString();
        }
        if (DDL_KH.SelectedValue.ToString() != "") {
            strMaKH = DDL_KH.SelectedValue.ToString();
        }
        int soluong = Convert.ToInt32(txtsoluong.Text.ToString());
        string ngay = txtngay.Text;
        string strMaDH = txtMaDH.Text;
        if (ExistsMaDonHangHang(strMaDH) != true)
        {
            SqlCommand comm = new SqlCommand("InsertDonHang", da.SqlConn());
            comm.CommandType = CommandType.StoredProcedure;
            comm.Parameters.AddWithValue("@MaDonHang", strMaDH);
            comm.Parameters.AddWithValue("@MaKH", strMaKH);
            comm.Parameters.AddWithValue("@MaHangHoa", strMaHang);
            comm.Parameters.AddWithValue("@NgayDatHang", ngay);
            comm.Parameters.AddWithValue("@SoLuong", soluong);

            comm.Connection.Open();
            if (comm.ExecuteNonQuery() == -1)
            {
                Response.Redirect("DanhSachDonHang.aspx");
            }

            comm.Connection.Close();
            comm.Connection.Dispose();
            Response.Redirect("DanhSachDonHang.aspx");

        }
        else
        {
            HttpContext.Current.Response.Write("<script language=\"javascript\">alert('Mã đơn hàng này đã tồn tại !');</script>");
        }

        
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        txtngay.Text = Calendar1.SelectedDate.ToString("dd-MM-yyyy");
        Calendar1.Visible = false;
    }   
    protected void lbCalendar_Click(object sender, EventArgs e)
    {
        Calendar1.Visible = true;
    }

    protected void btnSua_Click(object sender, EventArgs e)
    {
        insertDonHang();
    }
}
